package com.example.day2.zhibo;

import static android.content.ContentValues.TAG;

import android.util.Log;
import android.view.View;

import androidx.lifecycle.Observer;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.blankj.utilcode.util.ToastUtils;
import com.example.day2.R;
import com.example.day2.adapter.MassageAdapter;
import com.example.day2.databinding.ActivityZhibaoBinding;
import com.example.day2.databinding.ActivityZhibo2Binding;
import com.example.day2.entity.StopResult;
import com.example.day2.mvvm.base.BaseActivity;
import com.hyphenate.EMMessageListener;
import com.hyphenate.EMValueCallBack;
import com.hyphenate.chat.EMClient;
import com.hyphenate.chat.EMConversation;
import com.hyphenate.chat.EMCursorResult;
import com.hyphenate.chat.EMMessage;
import com.hyphenate.chat.EMTextMessageBody;
import com.tencent.rtmp.TXLivePushConfig;
import com.tencent.rtmp.TXLivePusher;
import com.tencent.rtmp.ui.TXCloudVideoView;

import java.util.List;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;


public class ZhiboActivity extends BaseActivity<ActivityZhibo2Binding,StartViewModel> {
    private MassageAdapter massageAdapter = new MassageAdapter(R.layout.item_message);
    String startMsgId = "";
    @Override
    public void initView() {

        TXLivePushConfig mLivePushConfig  = new TXLivePushConfig();
        TXLivePusher mLivePusher = new TXLivePusher(this);

        // 一般情况下不需要修改 config 的默认配置
        mLivePusher.setConfig(mLivePushConfig);

        //启动本地摄像头预览
        TXCloudVideoView mPusherView = binding.pusherTxCloudView;
        mLivePusher.startCameraPreview(mPusherView);

        //开播推流
        String rtmpURL = getIntent().getStringExtra("url"); //此处填写您的 rtmp 推流地址
        int ret = mLivePusher.startPusher(rtmpURL.trim());
        if (ret == -5) {
            Log.i(TAG, "startRTMPPush: license 校验失败");
        }

        //下播
        binding.btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mLivePusher.stopPusher();
                mLivePusher.stopCameraPreview(true); //如果已经启动了摄像头预览，请在结束推流时将其关闭。
                viewModel.getStop();
            }
        });
        //信息
        //设置信息列表
        binding.msg.setLayoutManager(new LinearLayoutManager(this));
        binding.msg.setAdapter(massageAdapter);
//        EMMessageListener msgListener = new EMMessageListener() {
//
//            // 收到消息，遍历消息队列，解析和显示。
//
        //定时器

        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                EMClient.getInstance().chatManager().asyncFetchHistoryMessage("string",
                        EMConversation.EMConversationType.Chat,
                        10,
                        startMsgId, new EMValueCallBack<EMCursorResult<EMMessage>>() {
                    @Override
                    public void onSuccess(EMCursorResult<EMMessage> value) {
                        for (EMMessage datum : value.getData()) {
                            System.out.println("=========="+datum.getMsgId());
                            EMTextMessageBody body = (EMTextMessageBody) datum.getBody();
                            String message1 = body.getMessage();
                            massageAdapter.getData().add(message1);

                        }
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                massageAdapter.notifyDataSetChanged();
                                binding.msg.scrollToPosition(massageAdapter.getData().size()-1);
                            }
                        });

                    }

                    @Override
                    public void onError(int error, String errorMsg) {
                        ToastUtils.showLong("失败了");
                    }
                });
            }
        },1000,1000);
    }

    @Override
    public void initData() {
        viewModel.successStop.observe(this, new Observer<StopResult>() {
            @Override
            public void onChanged(StopResult stopResult) {
                ToastUtils.showLong(stopResult.getMessage());
            }
        });

    }

    @Override
    public Class<StartViewModel> getVIewModel() {
        return StartViewModel.class;
    }

    @Override
    public int bindLayoutId() {
        return R.layout.activity_zhibo2;
    }
}
